package cn.myfirefly.moming.portal.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.myfirefly.moming.portal.dao.mapper.FunctionActionMapper;
import cn.myfirefly.moming.portal.entity.MMFunctionAction;
import cn.myfirefly.moming.portal.model.FunctionActionDto;
import cn.myfirefly.moming.portal.model.FunctionActionFilterDto;
import cn.myfirefly.moming.portal.model.FunctionPermissionDto;
import cn.myfirefly.core.BaseDao;
import cn.myfirefly.core.model.PageResultDto;
import cn.myfirefly.core.mybatis.BjuiDataGridFilter;
import cn.myfirefly.core.mybatis.BjuiDataGridSqlHelper;
import cn.myfirefly.utils.StrUtil;

@Repository
public class FunctionActionDao extends BaseDao<MMFunctionAction> {
	
	@Autowired
	private FunctionActionMapper functionActionMapper;
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public PageResultDto<FunctionActionDto> getList(FunctionActionFilterDto dto, Integer funSn) {
		PageResultDto<FunctionActionDto> resultDto = new PageResultDto();
		Map<String, Object> paraMap = new HashMap<>();
		paraMap.put("funSn", funSn);
		BjuiDataGridFilter bjuiDataGridFilter = BjuiDataGridSqlHelper.addFilter(dto);
		paraMap.putAll(bjuiDataGridFilter.getParam());
		//分页处理
		Page<FunctionActionDto> page = PageHelper.startPage(dto.getPageCurrent(), dto.getPageSize(), true);
		List<FunctionActionDto> list = functionActionMapper.getList(paraMap);
		resultDto.setPageCurrent(dto.getPageCurrent());
		resultDto.setTotal((int)page.getTotal());
		resultDto.setList(list);
		return resultDto;
	}
	
	public List<FunctionActionDto> getByLoginName(String loginName) {
		List<FunctionActionDto> results = functionActionMapper.getByLoginName(loginName);
		return results;
	}
	
	public List<FunctionActionDto> getByLoginName(Integer systemId, String loginName) {
		List<FunctionActionDto> results = functionActionMapper.getBySysIdLoginName(systemId, loginName);
		return results;
	}
	
	public FunctionPermissionDto getFunctionPermission(String funId, Integer roleSn) {
		List<Map<String, Object>> list = functionActionMapper.getFunctionPermission(funId, roleSn);
		FunctionPermissionDto results = new FunctionPermissionDto();
		FunctionActionDto dto = null;
		for (Map<String, Object> map : list) {
			results.setFunSn((Integer)map.get("fun_sn"));
			results.setFunId((String)map.get("fun_id"));
			results.setFunName((String)map.get("fun_name"));
			results.setParentFunId((String)map.get("parent_fun_id"));
			results.setLinkPage((String)map.get("link_page"));
			results.setDisplayFlag(map.get("display_flag").toString());
			results.setFunIndex((Integer)map.get("fun_index"));
			results.setFunDescription((String)map.get("fun_description"));
			dto = new FunctionActionDto();
			dto.setFunActionSn((Integer)map.get("fun_action_sn"));
			dto.setAction((String)map.get("action"));
			dto.setActionName((String)map.get("action_name"));
			dto.setStatus((String)map.get("status"));
			dto.setSelected(map.get("selected") == null ? false : true);
			if (StrUtil.equals("R", dto.getStatus())) {
				results.getFunReadList().add(dto);
			} else if (StrUtil.equals("W", dto.getStatus())) { 
				results.getFunWriteList().add(dto);
			}
			if(map.get("permission") == null){
				results.setFunPermission("N");
			} else {
				results.setFunPermission(map.get("permission").toString());
			}
		}
		return results;
	}
}
